package com.github.andy0106.kersstjtl.api;

import com.github.andy0106.kersstjtl.files.Dir;
import com.github.andy0106.kersstjtl.files.File;

import java.nio.file.Paths;
import java.util.ArrayList;

public class Package {
    private Long id;
    private String name;
    private String pack_version;
    private String game_version;
    private ArrayList<Mod> mods;
    private ArrayList<String> mess_mods;
    private ArrayList<TargetFile> files;
    private ArrayList<TargetDir> dirs;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPack_version() {
        return pack_version;
    }

    public void setPack_version(String pack_version) {
        this.pack_version = pack_version;
    }

    public String getGame_version() {
        return game_version;
    }

    public void setGame_version(String game_version) {
        this.game_version = game_version;
    }

    public ArrayList<Mod> getMods() {
        return mods;
    }

    public void setMods(ArrayList<Mod> mods) {
        this.mods = mods;
    }

    public ArrayList<TargetFile> getFiles() {
        return files;
    }

    public void setFiles(ArrayList<TargetFile> files) {
        this.files = files;
    }

    public ArrayList<TargetDir> getDirs() {
        return dirs;
    }

    public void setDirs(ArrayList<TargetDir> dirs) {
        this.dirs = dirs;
    }

    public String getFilename() {
        return name + "-" + pack_version + "-" + game_version + ".pack";
    }

    public ArrayList<String> getMess_mods() {
        return mess_mods;
    }

    public void setMess_mods(ArrayList<String> mess_mods) {
        this.mess_mods = mess_mods;
    }

    public boolean loadPackage() {
        Package pack = ApiBuffer.getLatestPackageByName(this.name);
        if (pack == null) pack = ApiBuffer.get_package_config(this);
        if (pack == null) return false;
        this.dirs = pack.dirs;
        this.files = pack.files;
        this.mods = pack.mods;
        this.mess_mods = pack.mess_mods;
        return true;
    }

    public boolean install_without_load(String rootdir) {
        Dir packdir = new Dir(name, rootdir);
        packdir.create();
        File packfile = new File(name + "-" + pack_version + "-" + pack_version + ".pack", packdir.toString());
        packfile.create();
        packfile.writeString_UTF_8(ApiBuffer.getLatestPackageJsonByName(name));
        Dir modsdir = new Dir(Paths.get(packdir.getPaths().toString(), "mods/"));
        modsdir.create();

        loadPackage();

        dirs.forEach(targetDir -> {
            targetDir.create(packdir);
        });

        mess_mods.forEach(name -> {
            ApiBuffer.download_messmod(name, modsdir.toString());
        });

        mods.forEach(mod -> {
            mod.download(modsdir);
        });

        files.forEach(targetFile -> {
            targetFile.download(name, packdir);
        });
        return true;
    }

    public boolean install(String rootdir) {
        return install_without_load(rootdir);
    }
}
